/**
 * 日期处理
 * 2024-08-04T07:11:55.000+0000 => 2024-08-04
 */
function formatDate(dateString) {
	// 创建一个 Date 对象
	const date = new Date(dateString);
	// 使用 toISOString 方法将日期转换为 ISO 8601 格式
	const isoDate = date.toISOString();
	// 提取日期部分
	const formattedDate = isoDate.split('T')[0];
	return formattedDate
}

/**
 * 获取当前日期 2024-08-24、2024/08-24等格式
 */
function getCurrentDate(format = "-") {
	// 获取当前时间
	const now = new Date();
	// 获取当前的年月日
	const year = now.getUTCFullYear();
	const month = String(now.getUTCMonth() + 1).padStart(2, "0"); // 月份是从0开始的，所以要加1
	const day = String(now.getUTCDate()).padStart(2, "0");
	return `${year}${format}${month}${format}${day}`;
}

function getUTCDate() {
	// 获取当前时间
	const now = new Date();

	// 获取当前的年月日
	const year = now.getUTCFullYear();
	const month = String(now.getUTCMonth() + 1).padStart(2, '0'); // 月份是从0开始的，所以要加1
	const day = String(now.getUTCDate()).padStart(2, '0');

	// 生成 UTC 时间格式
	const utcDateString = `${year}-${month}-${day}T00:00:00.000Z`;
	return utcDateString;
}

/**
 * 获取当前天UTC格式的日期 2024-08-24 T15:59:59.128Z，以及当前天的上一天 2024-08-23 T16:00:00.122Z
 */
function getCurrentAndPreviousDayUTC() {
	// 获取当前日期
	const now = new Date();

	// 获取当前日期的年月日
	const year = now.getUTCFullYear();
	const month = String(now.getUTCMonth() + 1).padStart(2, '0'); // 月份从0开始，所以需要加1
	const day = String(now.getUTCDate()).padStart(2, '0');

	// 构造当前日期的 UTC 时间字符串
	const currentUTCDate = `${year}-${month}-${day}T15:59:59.128Z`;

	// 获取上一天的日期
	const previousDay = new Date(now);
	previousDay.setUTCDate(now.getUTCDate() - 1); // 设置为前一天

	// 获取上一天的年月日组件
	const prevYear = previousDay.getUTCFullYear();
	const prevMonth = String(previousDay.getUTCMonth() + 1).padStart(2, '0');
	const prevDay = String(previousDay.getUTCDate()).padStart(2, '0');

	// 构造上一天的 UTC 时间字符串
	const previousUTCDate = `${prevYear}-${prevMonth}-${prevDay}T16:00:00.122Z`;

	return {
		currentUTCDate,
		previousUTCDate
	};
}

/**
 * 将 UTC 格式的日期字符串转换为 '年-月-日 时:分:秒' 格式的字符串
 * @param {string} utcDateString - UTC 格式的日期字符串，例如 '2024-08-24T09:09:08.000+0000'
 * @returns {string} - 格式化后的日期时间字符串，例如 '2024-08-24 09:09:08'
 * 使用示例
 * const utcDateString = '2024-08-24T09:09:08.000+0000';
 * const formattedDate = formatUTCDateToCustomFormat(utcDateString);
 * console.log(formattedDate); // 输出：2024-08-24 09:09:08
 */
function formatUTCDateToCustomFormat(utcDateString, type = 1) {
	// 解析 UTC 日期字符串，创建 Date 对象
	const date = new Date(utcDateString);

	// 获取日期组件
	const year = date.getUTCFullYear();
	const month = String(date.getUTCMonth() + 1).padStart(2, '0'); // 月份从0开始，所以需要加1
	const day = String(date.getUTCDate()).padStart(2, '0');
	const hours = String(date.getUTCHours()).padStart(2, '0');
	const minutes = String(date.getUTCMinutes()).padStart(2, '0');
	const seconds = String(date.getUTCSeconds()).padStart(2, '0');

	// 生成目标格式的字符串
	if (type == 1) {
		return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
	} else if (type == 2) {
		return `${year}-${month}-${day} ${hours}:${minutes}`;
	}
}

/**
 * 将毫秒时间戳转成日期格式
 * @param {Object} millis 毫秒时间戳
 */
function millisToDateString(millis, type = 1) {
  const date = new Date(millis);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0'); // 将月份（从0开始）转换为两位，不足前面补0
  const day = String(date.getDate()).padStart(2, '0'); // 将天数转换为两位，不足前面补0
	const hour = String(date.getHours()).padStart(2, '0');
	const minute = String(date.getMinutes()).padStart(2, '0');
	const second = String(date.getSeconds()).padStart(2, '0');
  if (type === 1) {
		return `${year}-${month}-${day}`;
	} else if (type === 2) {
		return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
	}
}

/**
 * 将毫秒时间戳转换为 UTC 格式的时间字符串
 * @param {number} millis - 毫秒时间戳
 * @returns {string} - UTC 格式的时间字符串，例如 '2024-08-24T09:09:08.000Z'
 */
function millisToUTCString(millis) {
  const date = new Date(millis);

  // 获取各个时间部分
  const year = date.getUTCFullYear();
  const month = String(date.getUTCMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1
  const day = String(date.getUTCDate()).padStart(2, '0');
  const hours = String(date.getUTCHours()).padStart(2, '0');
  const minutes = String(date.getUTCMinutes()).padStart(2, '0');
  const seconds = String(date.getUTCSeconds()).padStart(2, '0');
  const milliseconds = String(date.getUTCMilliseconds()).padStart(3, '0');

  // 生成 UTC 格式的字符串
  return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${milliseconds}Z`;
}


function formatPickerDate(dateString) {
  // 确保输入的日期字符串长度正确
  if (dateString.length !== 8) {
    throw new Error('Invalid date format');
  }

  // 提取年、月、日
  const year = dateString.substring(0, 4);
  const month = dateString.substring(4, 6);
  const day = dateString.substring(6, 8);

  // 格式化成 yyyy-MM-dd
  return `${year}-${month}-${day}`;
}

/**
 * 获取当天日期，格式 2024-09-11
 */
function getFormattedCurrentDate(type = 1) {
	const now = new Date();
	const year = now.getFullYear();
	const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1，并且格式化为2位
	const day = String(now.getDate()).padStart(2, '0'); // 日期格式化为2位
	const hour = String(now.getHours()).padStart(2, '0');
	const minute = String(now.getMinutes()).padStart(2, '0');
	const second = String(now.getSeconds()).padStart(2, '0');
	if (type === 1) {
		return `${year}-${month}-${day}`;
	} else if (type === 2) {
		return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
	} else if (type === 3) {
		return `${year}-${month}-${day} 00:00:00`;
	} else if (type === 4) {
		return `${year}-${month}-${day} 23:59:59`;
	}
}

export {
	formatDate,
	getCurrentDate,
	getUTCDate,
	getCurrentAndPreviousDayUTC,
	formatUTCDateToCustomFormat,
	millisToDateString,
	millisToUTCString,
	formatPickerDate,
	getFormattedCurrentDate,
}